package com.cote.douhahou.pretloan.service;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;

import com.cote.douhahou.pretloan.beans.AppAkomapalendConfigCustomize;
import com.cote.douhahou.pretloan.network.HttpHelper;
import com.cote.douhahou.pretloan.utils.AkomapaLendUtil;
import com.cote.douhahou.pretloan.R;

import java.util.Random;


public class CusServiceAkomapalendDialog extends Dialog {

    private LinearLayout cserItemGroup;
    private Runnable runnableOnDismoss;

    public CusServiceAkomapalendDialog(@NonNull Context context) {
        super(context);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_service);

        getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        getWindow().setBackgroundDrawableResource(R.color.transparent);
        setCanceledOnTouchOutside(false);
        getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

        cserItemGroup = findViewById(R.id.dcsa_layout_group);

        firstRun();
    }

    @SuppressLint("SetTextI18n")
    private void firstRun(){

        findViewById(R.id.dcsa_ib_close).setOnClickListener(v -> dismiss());

        String hint = AppAkomapalendConfigCustomize.instance().getCustomerServiceHint_akomapalend();
        String[] mobiles = AppAkomapalendConfigCustomize.instance().getMobiles_akomapalend();
        String[] emails = AppAkomapalendConfigCustomize.instance().getEmails_akomapalend();

        TextView descTv = findViewById(R.id.dcsa_tv_tips);
        if (!TextUtils.isEmpty(hint)) descTv.setText(hint);

        for (int i=0;i<mobiles.length;i++){
            String mobile = mobiles[i];
            addCsMobile(mobile, (mobiles.length<=1 ? -1:i+1));
        }

        for (int i=0;i<emails.length;i++){
            String email = emails[i];
            addAkomapalendEmail(email, (emails.length<=1 ? -1:i+1));
        }

    }

    @SuppressLint("SetTextI18n")
    private void addCsMobile(String mobile, int num){
        if (TextUtils.isEmpty(mobile)) return;
        View mobileItem = View.inflate(getContext(),R.layout.item_service,null);

        TextView titleTtv = mobileItem.findViewById(R.id.icsa_tv_title);
        TextView contentTextview = mobileItem.findViewById(R.id.icsa_tv_content);
        View copyButton = mobileItem.findViewById(R.id.icsa_btn_copy);
        View dialBtn = mobileItem.findViewById(R.id.icsa_btn_dial);

        dialBtn.setVisibility(View.VISIBLE);

        titleTtv.setText(getContext().getString(R.string.customer_service_phone_number) + (num>0 ? " "+num:""));
        contentTextview.setText(mobile);

        copyButton.setOnClickListener(v -> {
            HttpHelper.inst().submitEvent("DIALOG_CUSTOMER_SERVICE","PHONE_NUMBER_COPY","");
            ((ClipboardManager) getContext().getSystemService(Context.CLIPBOARD_SERVICE)).setPrimaryClip(ClipData.newPlainText("Label", mobile.trim()));
            Toast.makeText(getContext(), getContext().getString(R.string.tips_copy_success), Toast.LENGTH_SHORT).show();
        });

        dialBtn.setOnClickListener(v -> {
            HttpHelper.inst().submitEvent("DIALOG_CUSTOMER_SERVICE","PHONE_CALL","");
            Intent dialIntent =  new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + mobile.trim()));//跳转到拨号界面，同时传递电话号码
            getContext().startActivity(dialIntent);
        });

        cserItemGroup.addView(mobileItem);
    }

    @SuppressLint("SetTextI18n")
    private void addAkomapalendEmail(String email, int num){
        if (TextUtils.isEmpty(email)) return;

        View emailItem = View.inflate(getContext(),R.layout.item_service,null);

        TextView titleTv = emailItem.findViewById(R.id.icsa_tv_title);
        TextView contentTv = emailItem.findViewById(R.id.icsa_tv_content);
        View copyBtn = emailItem.findViewById(R.id.icsa_btn_copy);
        View dialBtn = emailItem.findViewById(R.id.icsa_btn_dial);

        dialBtn.setVisibility(View.GONE);

        titleTv.setText(getContext().getString(R.string.customer_service_email) + (num>0 ? " "+num:""));
        contentTv.setText(email);

        copyBtn.setOnClickListener(v -> {
            HttpHelper.inst().submitEvent("DIALOG_CUSTOMER_SERVICE","EMAIL_COPY","");
            ((ClipboardManager) getContext().getSystemService(Context.CLIPBOARD_SERVICE)).setPrimaryClip(ClipData.newPlainText("Label", email.trim()));
            Toast.makeText(getContext(), getContext().getString(R.string.tips_copy_success), Toast.LENGTH_SHORT).show();
        });

        cserItemGroup.addView(emailItem);
    }

    @Override
    public void show() {
        show(null);
        run(() -> HttpHelper.inst().submitEvent("DIALOG_CUSTOMER_SERVICE","START",""));
    }

    public void show(Runnable dismissRunnable) {
        super.show();
        this.runnableOnDismoss = dismissRunnable;
        HttpHelper.inst().submitEvent("DIALOG_CUSTOMER_SERVICE","START","");
    }

    @Override
    public void dismiss() {
        super.dismiss();
        HttpHelper.inst().submitEvent("DIALOG_CUSTOMER_SERVICE","END","");
        if (runnableOnDismoss !=null) runnableOnDismoss.run();
    }

    private void run(Runnable runnable){
        int x = new Random().nextInt();
        if (x>0) ++x;
        else x=1;
        AkomapaLendUtil.akomapaLendRun(runnable,x);
    }

}
